package stock;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.LogFactory;
import org.aspectj.bridge.Message;
import org.hibernate.Session;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.LoggerFactory;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.ScriptException;
import com.gargoylesoftware.htmlunit.ScriptResult;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.javascript.DebugFrameImpl;
import com.gargoylesoftware.htmlunit.javascript.JavaScriptErrorListener;
import com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob;

import ch.qos.logback.classic.LoggerContext;

import org.apache.commons.logging.*;

//org.apache.commons.logging.simplelog.Defaultlog
import org.apache.commons.logging.impl.NoOpLog;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import net.sf.json.JSONArray;

@SuppressWarnings("all")
public class Test {
	
	
	
	public static float calculateDistanceRatio(List<Float> list) {
        if (list.isEmpty()) {
            return -1; // Return -1 as an indication of error or invalid input
        }

        Float min = list.get(0);
        // Find the minimum value in the entire list
        for (Float num : list) {
            if (num < min) {
                min = num;
            }
        }

        // Calculate the distance ratio between list.get(0) and the minimum value
        float distance = Math.abs(list.get(0) - min);
        float ratio = distance / list.get(0);

        return ratio;
    }
	 public static void main(String[] args) {
	        // Test the method with a sample list
	        List<Float> myList = List.of(8.0f, 9.6f, 8.5f, 11.0f);

	        float ratio = calculateDistanceRatio(myList);
	        System.out.println("The ratio of list.get(0) to the minimum value in the list is: " + ratio);
	    }


	 public static void main11(String[] args) {
		 
		 System.out.println((3f-5f)/5f);
		 
		 
		// System.out.println(0 % 5 == 0);
		 
		 
		 
	    }

	public static void main7(String[] args) throws ParseException {
		// System.out.println(Common.getTimeLong("2019-03-01 00:00:00"));
		// DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
		// System.out.println(format1.parse("2019-03-01").getTime());

		// System.out.println(Common.getTime(1583078400000L));

		// StockCodeCommon.getTitle("600619");
	}

	public static void main5(String[] args) throws Exception {
		File f = new File("c:/stockdown/002600.csv");
		FileInputStream fileInputStream = null;
		fileInputStream = new FileInputStream(f);
		WorkbookSettings workbookSettings = new WorkbookSettings();
		workbookSettings.setEncoding("UTF-8");
		Workbook wb = Workbook.getWorkbook(fileInputStream, workbookSettings);
		// Excel的页签数量
		int sheet_size = wb.getNumberOfSheets();
		for (int index = 0; index < sheet_size; index++) {
			// 每个页签创建一个Sheet对象
			Sheet sheet = wb.getSheet(index);
			// sheet.getRows()返回该页的总行数
			for (int i = 0; i < sheet.getRows(); i++) {
				// sheet.getColumns()返回该页的总列数
				for (int j = 0; j < sheet.getColumns(); j++) {
					String cellinfo = sheet.getCell(j, i).getContents();
					System.out.println(cellinfo);
				}
			}
		}

	}

	public static void saveCode1(Iterable<DomNode> list1) {
		for (DomNode domNode : list1) {
			List<DomNode> listTD = domNode.getChildNodes();
			System.out.println(domNode.asNormalizedText());
			/// StockCode sc = new StockCode();
			for (int i = 0; i < listTD.size(); i++) {
				String string = listTD.get(i).asNormalizedText();
				System.out.println(string);
//				 if(i==1) {
//					 sc.setSname(string); 
//				 }
//				 if(i==2) { 
//					 sc.setScode(string); 
//				 } 
//				 if(i==4) {
//					 sc.setDate1(string);
//				 } 
			}
		}
	}

	// 得到深交所所有股票代码及综合测试 2022年3月18日
	public static void main6(String[] args) throws InterruptedException {

		System.setProperty("org.apache.commons.logging.LogFactory", "org.apache.commons.logging.impl.LogFactoryImpl");
		// LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log",
		// "org.apache.commons.logging.impl.NoOpLog");

		final WebClient webClient = new WebClient(BrowserVersion.CHROME);// 新建一个模拟谷歌Chrome浏览器的浏览器客户端对象
		webClient.getOptions().setThrowExceptionOnScriptError(false);// 当JS执行出错的时候是否抛出异常, 这里选择不需要
		webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);// 当HTTP的状态非200时是否抛出异常, 这里选择不需要
		webClient.getOptions().setActiveXNative(false);
		webClient.getOptions().setCssEnabled(false);// 是否启用CSS, 因为不需要展现页面, 所以不需要启用
		webClient.getOptions().setJavaScriptEnabled(true); // 很重要，启用JS

		webClient.setAjaxController(new NicelyResynchronizingAjaxController());// 很重要，设置支持AJAX
		HtmlPage page = null;
		try {
			page = webClient.getPage("http://www.szse.cn/market/product/stock/list/index.html");// 尝试加载上面图片例子给出的网页
		} catch (Exception e) {
			e.printStackTrace();
		}
		// webClient.setJavaScriptTimeout(1000);
		webClient.waitForBackgroundJavaScript(3000);// 异步JS执行需要耗时,所以这里线程要阻塞30秒,等待异步JS执行结束

		List<HtmlElement> list = page.getByXPath("//tbody//tr");
		for (int i = 0; i < list.size(); i++) {
			HtmlElement tr = list.get(i);
			System.out.println(tr.asNormalizedText());
//			List<HtmlElement> listtd = tr.getByXPath("//td");
//			for (int j = 0; j < listtd.size(); j++) {
//				HtmlElement hl = listtd.get(j);
//				System.out.println(hl.asNormalizedText());
//			}
		}

		HtmlElement div = (HtmlElement) page.getByXPath("//li[@class='next' and @data-show='next']").get(0);
		HtmlElement button = (HtmlElement) div.getFirstChild();

		try {
			for (int i = 0; i < 130; i++) {
				// page = null;
				page = button.click();
				webClient.waitForBackgroundJavaScript(3000);
				List<HtmlElement> list1 = page.getByXPath("//tbody//tr");
				for (int j = 0; j < list1.size(); j++) {
					System.out.println(list1.get(j).asNormalizedText());
				}
				// div = (HtmlElement) page.getByXPath("//li[@class='next' and
				// @data-show='next']").get(0);
				// System.out.println(page.asNormalizedText());
				// saveCode1(div.getChildren());
				Thread.sleep(4000);
				div = (HtmlElement) page.getByXPath("//li[@class='next' and @data-show='next']").get(0);
				button = (HtmlElement) div.getFirstChild();
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public HtmlElement getTable(HtmlPage page) {
		List<HtmlElement> list = page.getByXPath("//tbody//tr");
		for (int i = 0; i < list.size(); i++) {
			HtmlElement tr = list.get(i);
			List<HtmlElement> listtd = tr.getByXPath("//td");
			for (int j = 0; j < listtd.size(); j++) {
				HtmlElement hl = listtd.get(j);
				return hl;
			}
		}
		return null;
	}

	// 得到上交所有的股票代码2022年3月18日d
	public static void main14(String[] args) {
		System.setProperty("org.apache.commons.logging.LogFactory", "org.apache.commons.logging.impl.LogFactoryImpl");
		// LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log",
		// "org.apache.commons.logging.impl.NoOpLog");
		final WebClient webClient = new WebClient(BrowserVersion.CHROME);// 新建一个模拟谷歌Chrome浏览器的浏览器客户端对象
		webClient.getOptions().setThrowExceptionOnScriptError(false);// 当JS执行出错的时候是否抛出异常, 这里选择不需要
		webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);// 当HTTP的状态非200时是否抛出异常, 这里选择不需要
		webClient.getOptions().setActiveXNative(false);
		webClient.getOptions().setCssEnabled(false);// 是否启用CSS, 因为不需要展现页面, 所以不需要启用
		webClient.getOptions().setJavaScriptEnabled(true); // 很重要，启用JS
		webClient.setAjaxController(new NicelyResynchronizingAjaxController());// 很重要，设置支持AJAX
		HtmlPage page = null;
		try {
			page = webClient.getPage("http://www.sse.com.cn/assortment/stock/list/share/");// 尝试加载上面图片例子给出的网页

		} catch (Exception e) {
			e.printStackTrace();
		}
		// webClient.setJavaScriptTimeout(1000);
		webClient.waitForBackgroundJavaScript(3000);// 异步JS执行需要耗时,所以这里线程要阻塞30秒,等待异步JS执行结束
		Iterable<DomNode> list = page.getByXPath("//tbody//tr");
		HtmlElement div = null;
		HtmlElement button = null;
		saveCode(list);
		try {
			for (int i = 0; i < 70; i++) {
				div = (HtmlElement) (page.getByXPath("//li[@class='next']").get(0));
				button = (HtmlElement) div.getFirstChild();
				button.click();
				page = null;
				page = button.click();
				webClient.waitForBackgroundJavaScript(3000);
				Thread.sleep(4000);
				Iterable<DomNode> list1 = page.getByXPath("//tbody//tr");
				saveCode(list1);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// System.out.println(list.size());
	}

	static {
		LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
		List<ch.qos.logback.classic.Logger> loggerList = loggerContext.getLoggerList();
		for (int i = 0; i < loggerList.size(); i++) {
			loggerList.get(i).setLevel(ch.qos.logback.classic.Level.ERROR);
			;
		}
	}

	// save coode 上海的
	public static void saveCode(Iterable<DomNode> list1) {
		for (DomNode domNode : list1) {
			List<DomNode> listTD = domNode.getChildNodes();
			System.out.println(domNode.asNormalizedText());
			// StockCode sc = new StockCode();
			for (int i = 0; i < listTD.size(); i++) {
				String string = listTD.get(i).asNormalizedText();
//				 if(i==1) {
//					 sc.setSname(string); 
//				 }
//				 if(i==2) { 
//					 sc.setScode(string); 
//				 } 
//				 if(i==4) {
//					 sc.setDate1(string);
//				 } 
			}
		}
	}

	public static void main1(String[] args) {
		ExportExcel ee = new ExportExcel();
		File f = new File("C:\\stockdown\\300299.csv");
		// List<Model163> list = (List<Model163>) ee.parseCsv(f, Model163.class);
		// System.out.println(JSONArray.fromObject(list).toString());

	}

	public static void main13(String[] args) {

		System.out.println(Common.getTime(1642348800000L - 30 * 24 * 60 * 60 * 1000));

	}

}
